package com.ac.scum.log.web.model.wechat.mp.handler;

import com.ac.scum.log.web.model.wechat.mp.mapper.WeChatUserMapper;
import com.ac.scum.wechat.bean.WeChatUser;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

@Slf4j
@Component
@AllArgsConstructor
public class UnsubscribeHandler extends AbstractHandler {
    private final WeChatUserMapper weChatUserMapper;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) {
        WeChatUser weChatUser = new WeChatUser();
        weChatUser.setUnsubscribeTime(LocalDateTime.now());
        weChatUser.setIsSubscribe(false);
        weChatUserMapper.update(weChatUser, Wrappers.lambdaUpdate(WeChatUser.class).eq(WeChatUser::getOpenId, wxMessage.getFromUser()));
        log.info("取消关注用户 OPENID: " + wxMessage.getFromUser());
        return null;
    }
}
